package com.ruoyi.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.domain.News;
import com.ruoyi.domain.NewsWithType;
import com.ruoyi.domain.RangSearchObject;
import com.ruoyi.domain.common.PageResult;
import com.ruoyi.domain.common.ResponseResult;
import com.ruoyi.domain.common.UserHolder;
import com.ruoyi.domain.entity.NewsOrigin;
import com.ruoyi.service.INewsService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 新闻Controller
 *
 * @author ruoyi
 * @date 2025-01-04
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Resource
    private INewsService newsService;

    /**
     * 查询新闻列表
     */
//    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/list")
    public ResponseResult list(News news) {
        PageResult<NewsWithType> newsPageResult = new PageResult<>();
        List<NewsWithType> list = newsService.selectNewsList(news);
        newsPageResult.setTotal(list.size());
        newsPageResult.setRecords(list);
        return ResponseResult.success(newsPageResult);
    }

    /**
     * 导出新闻列表
     */
//    @PreAuthorize("@ss.hasPermi('news:news:export')")
//    @Log(title = "新闻", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, News news) {
//        List<NewsWithType> list = newsService.selectNewsList(news);
//        ExcelUtil<NewsWithType> util = new ExcelUtil<NewsWithType>(NewsWithType.class);
//        util.exportExcel(response, list, "新闻数据");
//    }

    /**
     * 获取新闻详细信息
     */
//    @PreAuthorize("@ss.hasPermi('news:news:query')")
    @GetMapping("/newsDetail")
    public ResponseResult getInfo(Integer id) {
        return ResponseResult.success(newsService.selectNewsById(id));
    }

    /**
     * 新增新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:add')")
//    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseResult add(@RequestBody News news) {
        return ResponseResult.success(newsService.insertNews(news));
    }

    /**
     * 修改新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:edit')")
//    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseResult edit(@RequestBody News news) {
        return ResponseResult.success(newsService.updateNews(news));
    }

    /**
     * 删除新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:remove')")
//    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResponseResult remove(@PathVariable Integer[] ids) {
        return ResponseResult.success(newsService.deleteNewsByIds(ids));
    }

    /**
     * 查询最新新闻
     */
    @GetMapping("/lastNews")
    public ResponseResult selectLastNews() {
        List<NewsWithType> list = newsService.selectLastNews();
        PageResult<NewsWithType> pageResult = new PageResult<>();
        pageResult.setRecords(list);
        pageResult.setTotal(list.size());
        return ResponseResult.success(pageResult);
    }

    @PostMapping ("/rangSearch")
    public ResponseResult newsRangSearch(@RequestBody RangSearchObject searchObject) {
        // 创建一个 RangSearchObject 对象并设置传入的参数
        if(searchObject.getSentiment()!=null&&searchObject.getSentiment().equals("all")){
            searchObject.setSentiment(null);
        }
        PageResult<NewsWithType> pageResult = newsService.newsRangSearch(searchObject);
        return ResponseResult.success(pageResult);
    }

    @GetMapping("/getOriginList")
    @Operation(summary = "获取新闻来源列表")
    public ResponseResult<List<NewsOrigin>> getNewsOriginList() {
        List<NewsOrigin> list = newsService.getNewsOriginList();
        return ResponseResult.success(list);
    }


    /**
     * 查询与新闻相关的最新三条数据
     */
    @GetMapping("/listThreeNews/{stockId}")
    public ResponseResult listThreeNews(@PathVariable("stockId") Integer stockId){
        if(stockId == null){
            return ResponseResult.error("股票ID未上传");
        }
        return ResponseResult.success(newsService.listThreeNews(stockId));
    }

    /**
     * 查询与自选股相关新闻(按时间排序)
     */
    @GetMapping("/listWatchlistNews")
    public ResponseResult listWatchlistNews(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        Integer userId = UserHolder.getUserId();
        if (userId == null || userId == 0) {
            return ResponseResult.error("用户未登录，请先登录");
        }
        PageResult<NewsWithType> pageResult = newsService.listWatchlistNews(userId, pageNo, pageSize);
        return ResponseResult.success(pageResult);
    }

    /**
     * 按关键字模糊查询新闻
     */
    @GetMapping("/searchByKeyWord")
    public ResponseResult searchNewsByKeyword(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        PageResult<NewsWithType> pageResult = newsService.searchNewsByKeyword(keyword, pageNo, pageSize);
        return ResponseResult.success(pageResult);
    }

    /**
     * 多条件查询新闻
     */
    @PostMapping ("/conditionsSearch")
    public ResponseResult conditionsSearch(@RequestBody RangSearchObject searchObject){
        // 调用服务层方法进行查询
        PageResult<NewsWithType> pageResult = newsService.conditionsSearch(searchObject);

        return ResponseResult.success(pageResult);
    }

}
